home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 1.2 KB | 64 lines | [TEXT/CWIE] |
- // FileReader.cp
-
- #ifndef FileReader_h
- #include "FileReader.h"
- #endif
- #ifndef Buffer_h
- #include "Buffer.h"
- #endif
- #ifndef FileAccessPath_h
- #include "FileAccessPath.h"
- #endif
- #ifndef FilePermissionError_h
- #include "FilePermissionError.h"
- #endif
- #ifndef FileLockError_h
- #include "FileLockError.h"
- #endif
-
- FileReader::FileReader( const FileAccessPath& theFile,
- uint32 position )
- : finished( false )
- {
- Assert( theFile.IsOpen() );
-
- ioParam.ioCompletion = 0;
- ioParam.ioRefNum = theFile.RefNum();
- ioParam.ioPosMode = fsFromStart;
- ioParam.ioPosOffset = position;
- }
-
- uint32 FileReader::operator>>( Buffer& buffer )
- {
- Assert( !Finished() );
-
- ioParam.ioBuffer = reinterpret_cast<char *>( buffer.Unused().Start() );
- ioParam.ioReqCount = buffer.Unused().Length();
-
- OSErr error = PBReadSync( this );
- buffer.AdvanceMark( ioParam.ioActCount );
-
- if ( error == eofErr )
- finished = true;
- else
- ThrowError( error );
-
- return ioParam.ioActCount;
- }
-
- void FileReader::ThrowError( OSErr error )
- {
- if ( error == noErr )
- return;
-
- switch ( error )
- {
- case afpAccessDenied: throw FilePermissionError( error );
-
- // This is a documented possibility: (?)
- case fLckdErr: throw FileLockError( error );
- }
-
- throw FileError( error );
- }
-